package me.zhengjie.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.biz.domain.Vip;
import me.zhengjie.modules.biz.domain.resp.PreCheckResp;
import me.zhengjie.modules.biz.domain.req.UniversitiesRecommendReq;
import me.zhengjie.modules.biz.mapper.VipMapper;
import me.zhengjie.modules.biz.service.CommonService;
import me.zhengjie.modules.system.domain.WxUser;
import me.zhengjie.modules.system.mapper.WxUserMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static me.zhengjie.modules.biz.constant.GlobalConstant.*;
import static me.zhengjie.modules.biz.domain.enums.ErrorCode.*;

@Service
@RequiredArgsConstructor
@Slf4j
public class CommonServiceImpl implements CommonService {

    private final WxUserMapper wxUserMapper;

    private final VipMapper vipMapper;


    @Override
    public PreCheckResp preCheck(UniversitiesRecommendReq req) throws BizException {
        PreCheckResp resp = new PreCheckResp();
        if(req == null || req.getUserId() == null || req.getCategory() == null || req.getGrossScore() == null){
            throw new BizException(BODY_NOTNULL.getCode(),BODY_NOTNULL.getMessage());
        }
        int category = req.getCategory();
        resp.setCategory(category);

        // 查询用户信息和vip配置信息
        WxUser wxUser = wxUserMapper.selectOne(new QueryWrapper<WxUser>().eq("id", req.getUserId()));
        if(wxUser == null){
            throw new BizException(USER_NOT_REGISTER.getCode(),USER_NOT_REGISTER.getMessage());
        }
        // 先判断用户是否为vip
        Vip vip = vipMapper.selectOne(new QueryWrapper<Vip>().eq("category", category));
        // 先查询是否属于免费期
        // 获取当前时间
        LocalDateTime currentDate = LocalDateTime.now();
        if(vip == null){
            throw new BizException(VIP_TYPE_ERROR.getCode(),VIP_TYPE_ERROR.getMessage());
        }
        // 判断当前时间是否在给定时间段内
        if (vip.getStartTime() != null && vip.getEndTime() != null
                && !currentDate.isBefore(vip.getStartTime()) && !currentDate.isAfter(vip.getEndTime())) {
            int maxRecommendNum;
            switch(category){
                case CATEGORY_GK:
                    // 非免费期间，普通用户跳转开通vip页面
                    if(wxUser.getIsTzVip() == 0 && wxUser.getTzRecommendNum() == 1){
                        resp.setMsg(OPEN_VIP_REMIND);
                        resp.setStatus(2);
                        return resp;
                    }
                    // 其他情况
                    maxRecommendNum = wxUser.getIsTzVip() == 1 ? 3 : 1;
                    if (wxUser.getTzRecommendNum() >= maxRecommendNum) {
                        throw new BizException(RECOMMEND_NMU_MAX.getCode(),RECOMMEND_NMU_MAX.getMessage());
                    }
                    break;
                case CATEGORY_ZSB:
                    // 非免费期间，普通用户跳转开通vip页面
                    if(wxUser.getIsZsbVip() == 0 && wxUser.getZsbRecommendNum() == 1){
                        resp.setMsg(OPEN_VIP_REMIND);
                        resp.setStatus(2);
                        return resp;
                    }
                    // 其他情况
                    maxRecommendNum = wxUser.getIsVip() == 1 ? 3 : 1;
                    if (wxUser.getZsbRecommendNum() >= maxRecommendNum) {
                        throw new BizException(RECOMMEND_NMU_MAX.getCode(),RECOMMEND_NMU_MAX.getMessage());
                    }
                    break;
                case CATEGORY_DZ:
                    // 非免费期间，普通用户跳转开通vip页面
                    if(wxUser.getIsVip() == 0 && wxUser.getRecommendNum() == 1){
                        resp.setMsg(OPEN_VIP_REMIND);
                        resp.setStatus(2);
                        return resp;
                    }
                    maxRecommendNum = wxUser.getIsVip() == 1 ? 3 : 1;
                    if (wxUser.getRecommendNum() >= maxRecommendNum) {
                        throw new BizException(RECOMMEND_NMU_MAX.getCode(),RECOMMEND_NMU_MAX.getMessage());
                    }
                    break;
                default:
                    log.info("智能荐校 {} 模块 暂未开放",category);
            }
        }
        // todo 后续写入配置表进行判断 并添加最低省控线
        checkScoreScope(req, category);

        resp.setStatus(1);
        return resp;
    }

    private static void checkScoreScope(UniversitiesRecommendReq req, int category) throws BizException {
        // 获取智能荐校的类别和学生的总分
        Double grossScore = req.getGrossScore();
        // 1：统招
        if(category == CATEGORY_GK){
            if(grossScore < 0 || grossScore > 750){
                throw new BizException(TZ_GROSS_SCORE_ERROR.getCode(),TZ_GROSS_SCORE_ERROR.getMessage());
            }
            // 分数低于省控线
            if(req.getTzExamType() == 1 && grossScore < 458){
                throw new BizException(SCORE_UNDER_CONTROLLER_LINE.getCode(), SCORE_UNDER_CONTROLLER_LINE.getMessage());
            }
            if(req.getTzExamType() == 2 && grossScore < 433){
                throw new BizException(SCORE_UNDER_CONTROLLER_LINE.getCode(), SCORE_UNDER_CONTROLLER_LINE.getMessage());
            }
        }
        // 2：专升本
        if(category == CATEGORY_ZSB){
            if(grossScore < 0 || grossScore > 450){
                throw new BizException(ZSB_GROSS_SCORE_ERROR.getCode(),ZSB_GROSS_SCORE_ERROR.getMessage());
            }
        }
        // 3：单招
        if(category == CATEGORY_DZ){
            if(grossScore < 0 || grossScore > 500){
                throw new BizException(DZ_GROSS_SCORE_ERROR.getCode(),DZ_GROSS_SCORE_ERROR.getMessage());
            }
        }
    }
}
